<template>
  <div>
    <el-form :model="msg.data" :rules="rules" ref="form" label-position="left" label-width="30%" class="ml30">
      <el-form-item label="区名称：" prop="name">
        <el-input class="width80" :disabled="true" v-model="msg.data.name"></el-input>
      </el-form-item>
      <el-form-item label="授权学校数量：" prop="experimentNum">
        <el-input type="number" class="width80" :disabled="true" v-model="msg.data.experimentNum"></el-input>
      </el-form-item>
      <el-form-item label="示范学校数量：" prop="standoutNum">
        <el-input class="width80" :disabled="true" v-model="msg.data.standoutNum"></el-input>
      </el-form-item>
      <el-form-item label="学校数量：" prop="schoolNum">
        <el-input class="width80" v-model="msg.data.schoolNum"></el-input>
      </el-form-item>
      <el-form-item label="是否授权：" prop="isExperiment">
        <el-select class="width80" v-model="msg.data.isExperiment" placeholder="是否授权">
          <el-option label="是" :value="true"/>
          <el-option label="否" :value="false"/>
        </el-select>
      </el-form-item>
      <el-form-item label="加入年份：" prop="joinYear">
        <el-date-picker class="width80" :disabled="!msg.data.isExperiment" v-model="msg.data.joinYear" type="year" placeholder="加入年份"/>
      </el-form-item>
      <el-form-item label="联系人姓名：" prop="contact">
        <el-input :disabled="!msg.data.isExperiment" class="width80" v-model="msg.data.contact"></el-input>
      </el-form-item>
      <el-form-item label="联系人电话：" prop="contactPhone">
        <el-input :disabled="!msg.data.isExperiment" class="width80" v-model="msg.data.contactPhone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="contactEmail">
        <el-input :disabled="!msg.data.isExperiment" class="width80" v-model="msg.data.contactEmail"></el-input>
      </el-form-item>
      <el-form-item label="联系地址：" prop="contactAddress">
        <el-input :disabled="!msg.data.isExperiment" class="width80" v-model="msg.data.contactAddress" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
      </el-form-item>
      <el-form-item label="成为试验区时间：" prop="experimentAt">
        <el-date-picker :disabled="!msg.data.isExperiment" v-model="msg.data.experimentAt" type="date" class="width80" placeholder="选择成为试验区时间"/>
      </el-form-item>
      <el-form-item label="成为示范区时间：" prop="standoutAt">
        <el-date-picker :disabled="!msg.data.isExperiment" v-model="msg.data.standoutAt" type="date" class="width80" placeholder="选择成为示范区时间"/>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button class="el-button--blue" type="primary" @click="submitDistrictChange">保 存</el-button>
      <el-button @click="msg.visible=false">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { SubmitDistrictChange, awaitCheckUserName } from '../../../api/form'
export default {
  model: {
    prop: 'msg',
    event: 'cc'
  },
  name: 'district-change',
  props: {
    msg: {}
  },
  methods: {
    submitDistrictChange () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          SubmitDistrictChange(this.msg.data).then(res => {
            if (res.status !== 200) {
              this.$message.error('联系电话有误')
            } else {
              this.msg.visible = false
              this.$store.dispatch('SetFlash')
            }
          })
        } else {
          return false
        }
      })
    }
  },
  data () {
    let validateSchoolNum = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('必须输入学校数量'))
        return
      }
      value = Number(value)
      if (typeof value === 'number' && !isNaN(value)) {
        if (value < 0) {
          callback(new Error('学校数量不能小于0'))
        } else {
          callback()
        }
      } else {
        callback(new Error('学校数量必须为数字'))
      }
    }
    let validateJoinYear = (rule, value, callback) => {
      if (!value || value == null) {
        if (this.msg.data.joinYear) {
          callback(new Error('必须输入加入年份'))
        }
      } else if (new Date(value).getFullYear() <= 2000) {
        callback(new Error('年份必须大于2000'))
      }
      callback()
    }
    let validatePhone = (rule, value, callback) => {
      if (value.toString() !== '') {
        let res = awaitCheckUserName(value, 'area', this.msg.data.id)
        if (res.status === 200) {
          if (!res.responseJSON.result) {
            callback(new Error(res.responseJSON.msg))
          } else {
            callback()
          }
        }
      }
      callback()
    }
    return {
      rules: {
        schoolNum: [
          { validator: validateSchoolNum, trigger: 'change' }
        ],
        isExperiment: [
          { type: 'boolean', message: '请选择是否授权', trigger: 'change' }
        ],
        joinYear: [
          { validator: validateJoinYear, trigger: 'change' }
        ],
        contact: [
          { min: 0, max: 10, message: '长度在 10 个字符以内', trigger: 'change' }
        ],
        contactPhone: [
          { type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'change' },
          { type: 'string', pattern: /(^([0-9]{3,4}-)?[0-9]{7,8}$)|(^0?1[3|4|5|8][0-9]\d{8}$)/, message: '非法电话号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        contactEmail: [
          { type: 'string', pattern: /^([a-zA-Z]|[0-9])(\w|-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/, message: '非法邮箱', trigger: 'change' }
        ],
        contactAddress: [
          { min: 0, max: 255, message: '长度在 255 个字符以内', trigger: 'change' }
        ]
      }
    }
  }
}
</script>

<style scoped>
.width80 {
  width: 80%;
}
.footer {
  display: flex;
  justify-content: flex-end;
}
</style>
